package net.javamine.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

/**
 * 配置文件Properties工具类
 * 
 * @author <a href="mailto: java_lht@163.com">Lht</a>
 * @since 2012-5-24 1.create
 */
public class PropertiesUtils {
	
	private static Logger log = Logger.getLogger(PropertiesUtils.class);

	/**
	 * 获取指定properties文件中指定键的指定值
	 * 
	 * @param srcFilePath
	 * @param key
	 * @return value
	 */
	public static String readValue(String srcFilePath, String key) {
		if (StringUtils.hasText(srcFilePath) && StringUtils.hasText(key)) {
			try {
				Properties props = new Properties();
				InputStream in = new BufferedInputStream(new FileInputStream(
						srcFilePath));
				props.load(in);
				String value = props.getProperty(key);
				return value;
			} catch (Exception e) {
				ExceptionUtils.getExceptionMessage(log, e);
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 读取properties文件
	 * 
	 * @param srcFilePath
	 *            properties文件路径+文件名。格式：sourceFolder/package../fileName.properties
	 *            例如：properties/properties/hibernate.properties)
	 * @return map k,v
	 */
	public static Map<String, String> readProperties(String srcFilePath) {
		if (StringUtils.hasText(srcFilePath)) {
			Map<String, String> map = new LinkedHashMap<String, String>();
			try {
				Properties prop = new Properties();
				InputStream in = new BufferedInputStream(new FileInputStream(
						srcFilePath));
				prop.load(in);
				@SuppressWarnings("rawtypes")
				Enumeration enumeration = prop.propertyNames();
				String key = "";
				String value = "";
				while (enumeration.hasMoreElements()) {
					key = (String) enumeration.nextElement();
					value = prop.getProperty(key);
					map.put(key, value);
				}
			} catch (Exception e) {
				ExceptionUtils.getExceptionMessage(log, e);
				return null;
			}
			return map;
		} else {
			return null;
		}
	}

	/**
	 * 写入properties文件
	 * 
	 * @param targetFilePath
	 *            目标路径+文件名。格式：sourceFolder/package../fileName.properties
	 *            例如：properties/properties/hibernate.properties)
	 * @param map
	 *            装载需要写入文件的key 和 value 的map
	 */
	public static void writeProperties(String targetFilePath,
			Map<String, String> map) {
		try {
			Properties prop = new Properties();
			OutputStream outputStream = new FileOutputStream(targetFilePath);
			for (String key : map.keySet()) {
				String value = map.get(key);
				prop.setProperty(key, value);
			}
			// 将此 Properties 表中的属性列表（键和元素对）写入输出流
			prop.store(outputStream, ""); // 后面空着的字符串为注释
		} catch (Exception e) {
			ExceptionUtils.getExceptionMessage(log, e);
		}
	}

	public static void main(String[] args) {
		Map<String, String> map = readProperties("properties/properties/hibernate.properties");
		for (String s : map.keySet()) {
			System.out.println(s);
		}
		writeProperties("properties/properties/hibernate1.properties", map);
		String s2 = readValue("properties/properties/hibernate.properties", "hibernate.cache.use_query_cache");
		System.out.println(s2);
	}

}
